{% extends 'backend/userManage.html' %}
{% block action_title %}
    {{ super() }}
    &nbsp;&nbsp;|&nbsp;&nbsp;<span class="text-primary">修改</span>
{% endblock %}
{% block scripts %}<!--不能删,删掉会影响页签-->
<script type="text/javascript" src="{{ url_for("static",filename="extra/sweetalert.min.js") }}"></script>
{% endblock %}
{% block action_block %}
    <button class="btn btn-info come-back" onclick="window.location.href='/main/userManage'">返回</button>
{% endblock %}
{% block backend_content %}
    <div class="edit_userinfo">
        <div>

            <!-- Nav tabs -->
            <ul class="nav nav-tabs" role="tablist" id="editTabs">
                <li role="presentation" class="active"><a href="#baseInfo" aria-controls="home" role="tab"
                                                          data-toggle="tab">基本信息</a></li>
                <li role="presentation"><a href="#editPassword" aria-controls="profile" role="tab"
                                           data-toggle="tab">修改密码</a></li>
            </ul>

            <!-- Tab panes -->
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="baseInfo">
                    <div style="margin-top: 50px">
                        <form class="form-horizontal center-block" style="width: 50%" onsubmit="return false;">
                            <div class="form-group">
                                <label for="inputUsername" class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="inputUsername" placeholder="username"
                                           disabled/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputAlias" class="col-sm-2 control-label">用户昵称</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="inputAlias" placeholder="user alias"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="" class="col-sm-2 control-label">用户状态</label>
                                <div class="col-sm-10" style="margin-top: 6px">
                                    <div class="col-sm-3">
                                        <label>
                                            <input type="radio" name="user-status" id="editUserStatus1"
                                                   value="true"/>
                                            激活
                                        </label>
                                    </div>
                                    <div class="col-sm-3">
                                        <label>
                                            <input type="radio" name="user-status" id="editUserStatus2"
                                                   value="false"/>
                                            取消激活
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="footer clearfix">
                                <button class="btn btn-default pull-left editInfo-cancel">取消</button>
                                <button class="btn btn-info pull-right editInfo-confirm">确认</button>
                            </div>
                        </form>
                    </div>
                </div>
                <div role="tabpanel" class="tab-pane" id="editPassword">
                    <div style="margin-top: 50px">
                        <form class="form-horizontal center-block" style="width: 50%" onsubmit="return false;">
                            <div class="form-group">
                                <label for="inputPassword" class="col-sm-2 control-label">用户密码</label>
                                <div class="col-sm-10">
                                    <input type="password" class="form-control" id="inputPassword"
                                           placeholder="password"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="confirmPassword" class="col-sm-2 control-label">确认密码</label>
                                <div class="col-sm-10">
                                    <input type="password" class="form-control" id="confirmPassword"
                                           placeholder="confirm password"/>
                                </div>
                            </div>
                            <div class="footer clearfix">
                                <button class="btn btn-default pull-left editpassword-cancel">取消</button>
                                <button class="btn btn-info pull-right editpassword-confirm">确认</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <script>
        $(function () {
            //获取userId
            var userId = GetQueryString("uid");
            loadUserBaseInfo(userId);
            function loadUserBaseInfo(userId) {
                ajaxRequestJson("GET", "/main/user/" + userId, {}, function (data) {
                    console.log(data);
                    if (data.code === 0) {//获取信息成功
                        $("#inputUsername").val(data.data.user_name);
                        $("#inputAlias").val(data.data.user_alias);
                        $("input[name='user-status'][value='" + data.data.status + "']").attr("checked", true);
                    } else {
                        swal("糟糕!", data.msg, "error");
                    }
                }, function (error) {
                    swal("糟糕!", "请求失败！", "error");
                });
            }

            $(".editInfo-cancel").click(function () {
                userId = GetQueryString("uid");
                loadUserBaseInfo(userId);
            });
            //将编辑信息保存
            $(".editInfo-confirm").click(function () {
                var flag = $('input[name=\'user-status\']:checked').val() === "true" ? true : false;
                var obj = {
                    form_type: "base_info",
                    status: flag,
                    user_alias: $("#inputAlias").val()
                };
                ajaxRequestJson("PUT", "/main/user/" + userId, obj, function (data) {
                    if (data.code === 0) {
                        swal("该条数据已成功修改!", {
                            icon: "success"
                        }).then(function (confirm) {
                            if (confirm) {
                                loadUserBaseInfo(userId);
                            }
                        });

                    } else {
                        swal("糟糕!", data.msg, "error");
                    }
                }, function (error) {
                    swal("糟糕!", "请求失败！", "error");
                });

            });

            //修改密码
            $(".editpassword-confirm").click(function () {
                userId = GetQueryString("uid");
                if ($("#editPassword").val().replace(/(^\s*)|(\s*$)/g, "") && $("#confirmPassword").val().replace(/(^\s*)|(\s*$)/g, "")) {
                    swal("警告!", "请完善信息再提交！", "warning");
                    return false;
                }
                ajaxRequestJson("PUT", "/main/user/" + userId, {
                    form_type: "change_pwd",
                    pwd: $("#inputPassword").val(),
                    cpwd: $("#confirmPassword").val()
                }, function (data) {
                    if (data.code === 0) {
                        swal("恭喜！","密码修改成功！","success");
                        clearPassword();
                    } else {
                        swal("糟糕！",data.msg,"error");
                    }
                }, function (error) {
                    swal("糟糕！","请求出错！","error");
                });
            });

            $(".editpassword-cancel").click(function () {
                clearPassword();
            });
            function clearPassword() {
                $("#inputPassword").val("");
                $("#confirmPassword").val("");
            }

        });
    </script>
{% endblock %}